﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace StickTops
{

    /// <summary>
    /// Starter for stick top
    /// </summary>
    public partial class LoginForm : Form
    {

        private Point LastCursorPosition;
        private bool IsMouseDown = false;
        private Bitmap MinimizePic;
        private Bitmap ClosePic;
        private Bitmap MinimizePicOver;
        private Bitmap ClosePicOver;
        private Varables Vars;
        public bool IsClosed = false;

        

        /// <summary>
        /// Original constructor
        /// </summary>
        public LoginForm(ref Varables vars)
        {
            InitializeComponent();
            Vars = vars;
            MinimizePic = new Bitmap(Properties.Resources.bar_min);
            ClosePic = new Bitmap(Properties.Resources.bar_close);
            MinimizePicOver = new Bitmap(Properties.Resources.bar_min_over);
            ClosePicOver = new Bitmap(Properties.Resources.bar_close_over);
            lblVer.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(4);
            txtUsername.Text = Properties.Settings.Default.Username;
            txtPassword.Text = Properties.Settings.Default.Password;
            checkBox1.Checked = Properties.Settings.Default.AutoLogin;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {


        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Vars.UserName = txtUsername.Text;
            Vars.PassWord = txtPassword.Text;
            SaveSql save = new SaveSql(ref Vars);
            bool Working = save.LogIn(txtUsername.Text, txtPassword.Text);

            if (Working == true && save.State == SaveSql.LogInState.Success)
            {
                Vars.LoggedIn = true;
                this.Hide();

                Properties.Settings.Default.Username = txtUsername.Text;
                if (checkBox1.Checked)
                {
                    Properties.Settings.Default.AutoLogin = true;
                    Properties.Settings.Default.Password = txtPassword.Text;
                }
            }
            else
            {
                if (save.State == SaveSql.LogInState.ConnError)
                {
                    MessageBox.Show(Vars.eConn);
                    SaveFile saveFile = new SaveFile(ref Vars); saveFile.LoadFromFile();
                    this.Hide();
                }

                if (save.State == SaveSql.LogInState.WrongPass)
                {
                    MessageBox.Show(Vars.ePass);
                }

                if (save.State == SaveSql.LogInState.CorruptString)
                {
                   DialogResult LoadFromFileQuestion = MessageBox.Show(Vars.eCorrupt,"Error",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                   if (DialogResult == DialogResult.Yes)
                   {
                       SaveFile loadFile = new SaveFile(ref Vars);
                       if (loadFile.LoadFromFile())
                       {
                           this.Hide();
                       }
                       else
                       {
                           Vars.GetMainRef().MakeNewNote();
                       }
                    }
                 }

                 if (save.State == SaveSql.LogInState.OtherError)
                 {
                     MessageBox.Show(Vars.eUnknown + save.Message);
                 }
            }

            Properties.Settings.Default.Save();
        }
     

        #region "Move"

        private void picLoginText_MouseDown(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = true;
            this.LastCursorPosition = new Point(e.X, e.Y);
        }

        private void picLoginText_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseDown == true)
            {
                //Move the form
                this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));

                //Redraw the form//
                this.Invalidate();
            }
        }

        private void picLoginText_MouseUp(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = false;
        }

        private void picTopBar_MouseDown(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = true;
            this.LastCursorPosition = new Point(e.X, e.Y);
        }

        private void picTopBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseDown == true)
            {
                //Move the form
                this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));

                //Redraw the form//
                this.Invalidate();
            }
        }

        private void picTopBar_MouseUp(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = false;
        }

        private void picLeftConer_MouseDown(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = true;
            this.LastCursorPosition = new Point(e.X, e.Y);
        }

        private void picLeftConer_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseDown == true)
            {
                //Move the form
                this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));

                //Redraw the form//
                this.Invalidate();
            }
        }

        private void picLeftConer_MouseUp(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = false;
        }

        #endregion

        #region "iconsHover"

        private void picMinimize_MouseEnter(object sender, EventArgs e)
        {
            picMinimize.Image = MinimizePicOver;
        }

        private void picMinimize_MouseLeave(object sender, EventArgs e)
        {
            picMinimize.Image = MinimizePic;
        }

        private void picClose_MouseEnter(object sender, EventArgs e)
        {
            picClose.Image = ClosePicOver;
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            picClose.Image = ClosePic;
        }

        #endregion

        #region "Frame"

        private void picClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void picMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        #endregion

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            IsClosed = true;
        }

        

    }
}
